package com.heer.gymnasium.module.evaluate.views;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;

import com.bumptech.glide.Glide;
import com.heer.gymnasium.R;
import com.heer.gymnasium.api.ResponseData.ApiResponse;
import com.heer.gymnasium.api.callback.BaseCallback;
import com.heer.gymnasium.base.BaseRefreshActivity;
import com.heer.gymnasium.module.evaluate.beans.MemberItemBean;
import com.heer.gymnasium.module.evaluate.beans.MemberListResponseBean;
import com.heer.gymnasium.util.ImageUtil;
import com.heer.imagepick.DensityUtils;
//import com.mob.tools.utils.ReflectHelper;
import com.zhihuiyun.library.commonadapter.QuickTypeAdapter;
import com.zhihuiyun.library.commonadapter.ViewHolder;
import com.zhihuiyun.library.view.CircleImageView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by yan
 * on 2018/3/15
 */

public class MemberListActivity extends BaseRefreshActivity {
    private static final int RES_ID_LAYOUT = R.layout.layout_member_list;
    private static final int RES_ID_LIST = R.id.layout_member_list;
    private static final int RES_ID_REFRESH = R.id.refreshLayout;

    private static final int RES_ID_ITEM = R.layout.item_member;
    private static final int RES_ID_HEAD = R.id.item_member_head_iv;
    private static final int RES_ID_NAME = R.id.item_member_name_tv;
    private static final int RES_ID_STATE = R.id.item_member_state_tv;


    private List<MemberItemBean> data;
    private QuickTypeAdapter<MemberItemBean> adapter;
    private Activity $this;

    @BindView(RES_ID_LIST)
    ListView listView;
    private String orderId;

    @Override
    protected void init() {
        super.init();
        $this = this;
        setContentView(RES_ID_LAYOUT);
        setRefreshResId(RES_ID_REFRESH);
    }

    @Override
    protected void initView() {
        super.initView();
        data = new ArrayList<>();
        adapter = new QuickTypeAdapter<MemberItemBean>(this, data, RES_ID_ITEM) {
            @Override
            public void bindData4View(ViewHolder holder, final MemberItemBean data, int type, int pos) {
                holder.setText(RES_ID_NAME, data.getMemberName());
                if (data.isEval()) {
                    holder.setText(RES_ID_STATE, "已评价");
                    holder.setBg(RES_ID_STATE, getResources().getColor(R.color.transparent));
                    holder.setTextColor(RES_ID_STATE, getResources().getColor(R.color.colorTextNormal));
                } else {
                    holder.setText(RES_ID_STATE, "评价");
                    holder.setBg(RES_ID_STATE, R.drawable.yellow_button_bg);
                    holder.setTextColor(RES_ID_STATE, getResources().getColor(R.color.white));
                }
                holder.getView(RES_ID_STATE).setPadding(DensityUtils.dip2px($this, 5), DensityUtils.dip2px($this, 2),
                        DensityUtils.dip2px($this, 5), DensityUtils.dip2px($this, 2));
                ImageUtil.loadUrl($this, data.getHeadUrl(), R.drawable.ic_member_icon, (CircleImageView) holder.getView(RES_ID_HEAD));

                holder.setLis(RES_ID_STATE, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (!data.isEval()) {
                            EvaStoreActivity.startActivity($this, data.getId(), orderId, "4");
                        }
                    }
                });
            }
        };
        listView.setAdapter(adapter);
    }

    @Override
    protected void loadData(boolean loadMore) {
        super.loadData(loadMore);
        setRefresh(false);
        getFightMemberList();
    }

    private void setRefresh(boolean isRefreshing) {
        if (layoutRefresh.isRefreshing()) {
            layoutRefresh.setRefreshing(isRefreshing);
        }
    }

    private void getFightMemberList() {
        orderId = getIntent().getStringExtra(EXTRA_ID);
        if (TextUtils.isEmpty(orderId)) {
            return;
        }
        mAppHelper.apiService().getFightMemberList(orderId).enqueue(new BaseCallback<ApiResponse<List<MemberListResponseBean.Item>>>() {
            @Override
            public void onSuccess(Call<ApiResponse<List<MemberListResponseBean.Item>>> call, Response<ApiResponse<List<MemberListResponseBean.Item>>> response) {
                data.clear();
                data.addAll(MemberItemBean.dataChange(response.body().getData()));
                adapter.notifyDataSetChanged();
            }
        });
    }

    //
    public static void startActivity(Context context, String id) {
        Intent intent = new Intent(context, MemberListActivity.class);
        intent.putExtra(EXTRA_ID, id);
        context.startActivity(intent);
    }
}
